//
//  ViewController.m
//  WebSocketTest
//
//  Created by tanqilong on 16/8/9.
//  Copyright © 2016年 tanqilong. All rights reserved.
//

#import "ViewController.h"
#import <SocketRocket/SocketRocket.h>
#import "NetDao.h"
#import "XLWebSocketNetWorking.h"
#import "WebSocketDao.h"
#import "HttpDNSController.h"
#import "HttpController.h"

@interface ViewController () <SRWebSocketDelegate>
{
    NetDao * _baseNet;
    WebSocketDao * _wsDao;
}
@property (nonatomic,strong) SRWebSocket * webSocket;
@property (nonatomic,strong) XLWebSocketNetWorking * net;
@property (nonatomic,strong)XLHttpNetworking * httpNet;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    _webSocket = [[SRWebSocket alloc]initWithURL:[NSURL URLWithString:@"ws://115.29.193.48:8088"]];
//    _webSocket = [[SRWebSocket alloc]initWithURL:[NSURL URLWithString:@"ws://10.202.1.110:8089/hqapi/ws"]];
//    _webSocket.delegate = self;
//    [_webSocket open];
    
//    _net = [XLBaseNetWorking requestWebSocketWithUrl:@"ws://10.202.1.110:8089/hqapi/ws" Parameters:nil IndicatorView:self.view Connected:^{
//        NSLog(@"连接成功123");
//    } Message:^(NSObject *resulte) {
//        NSLog(@"%@",resulte);
//    } CancelLoading:nil LoadingError:nil];
    
//    _baseNet = [XLBaseNetWorking requestWithUrl:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213" Parameters:nil IndicatorView:self.view LoadingFinished:^(NSObject *resulte) {
//        NSLog(@"%@",resulte);
//    } CancelLoading:nil LoadingError:nil];
    
//    _baseNet = [[NetDao alloc]initWithParameters:nil IndicatorView:self.view LoadingFinished:^(NSObject *resulte) {
//        NSLog(@"%@",resulte);
//    } CancelLoading:nil LoadingError:nil];
    
    
    //websocket测试
//    _wsDao = [[WebSocketDao alloc]initWebSocketWithParameters:@[@{@"type":@"subHq",@"event":@"ticker",@"param":@{@"symbol":@"AG.NJS"}}] IndicatorView:nil Connected:^{
//        NSLog(@"已连接");
//    } Message:^(NSObject *resulte) {
//        NSLog(@"获取到结果");
//    } CancelLoading:^{
//        NSLog(@"已经取消");
//    } LoadingError:^(id resulte) {
//        
//    }];

    
//    HttpController * con = [[HttpController alloc] init];
//    [self.navigationController pushViewController:con animated:NO];
//    self.httpNet =
    [[XLHttpNetworking alloc]initWithUrl:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213" Parameters:nil IndicatorView:nil LoadingFinished:^(NSObject *resulte) {
        NSLog(@"%@",resulte);
    } CancelLoading:nil LoadingError:^(id resulte) {
        
    }];
}

#pragma mark --代理
-(void)webSocketDidOpen:(SRWebSocket *)webSocket {
    NSLog(@"websocket connected");
}

-(void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message {
    NSLog(@"websocket收到消息");
}

-(void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean {
    NSLog(@"websocket closed");
}

-(void)sendData {
    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:@[@{@"type":@"subHq",@"event":@"ticker",@"param":@{@"symbol":@"AG.NJS"}}] options:NSJSONWritingPrettyPrinted error:nil];
    NSString * jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    [_webSocket send:jsonString];
}

- (IBAction)sendButtonClicked:(id)sender {
    [_net sendMessage:@[@{@"type":@"subHq",@"event":@"ticker",@"param":@{@"symbol":@"AG.NJS"}}]];
}

- (IBAction)cancelButtonClicked:(id)sender {
    [_wsDao close];
}

- (IBAction)beNilButtonClicked:(id)sender {
    _wsDao = nil;
}
- (IBAction)openButtonClicked:(id)sender {
    [_wsDao reopen];
}
- (IBAction)reopenButtonClicked:(id)sender {
    [_wsDao reopen];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
